package com.gotkicry.asynctask_listview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Enzo Cotter on 2019/5/22.
 */
public class ImageLoader {
    private static final String TAG = "ImageLoader";
    private ImageView imageView;
    private String mUrl;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d(TAG, "handleMessage: F");
            if(imageView.getTag().equals(mUrl)){
                Log.d(TAG, "handleMessage: T");
                imageView.setImageBitmap((Bitmap) msg.obj);
            }
        }
    };

    public void showImageByThread(ImageView imageView, final String s){
        this.imageView = imageView;
        this.mUrl = s;
        new Thread(){
            @Override
            public void run() {
                super.run();
                Bitmap bitmap = getBitmapFromURL(s);
                Message m = Message.obtain();
                m.obj = bitmap;
                handler.sendMessage(m);
            }
        }.start();
    }

    public Bitmap getBitmapFromURL(String mUrl){
        Bitmap bitmap;
        InputStream is = null;
        try {
            URL url = new URL(mUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            is = new BufferedInputStream(connection.getInputStream());
            bitmap = BitmapFactory.decodeStream(is);
            connection.disconnect();
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    void showImageByAsynTask(ImageView iv,String s){
        new NewAsyncTask(iv,s).execute(s);
    }

    class NewAsyncTask extends AsyncTask<String,Void,Bitmap>{
        private ImageView imageView;
        private String mUrl;

        public NewAsyncTask(ImageView imageView, String mUrl) {
            this.imageView = imageView;
            this.mUrl = mUrl;
        }

        @Override
        protected Bitmap doInBackground(String...strings) {
            Log.d(TAG, "doInBackground: "+strings[0]);
            return getBitmapFromURL(strings[0]);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if(imageView.getTag().equals(mUrl)){
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}
